/**
 * 
 */
package common;

import java.util.Random;

import data.Data.BlockActionType;

/**
 * Simulates rolling of one or multiple dice. Singleton.
 * 
 * @author bogdan
 *
 */
public class Dice 
{	
	/**
	 * Rolls a single dice with the given number of faces. 
	 * @param sides - the number of sides of the dice. Minimum value = 2 (coin flip simulation).
	 * @return Returns a random value in the interval [1, sides]
	 */
	public static int rollDice(int sides) {
		if(sides < 2)
			return -1;
		
		return _generator.nextInt(sides) + 1;
	}
	
	/**
	 * Rolls a number of dice of the same number of sides. 
	 * @param sides - the number of faces of the dice.
	 * @param dice - the number of dice
	 * @return Returns a random value representing the sum of all the rolled dice. 
	 */
	public static int rollDice(int sides, int dice) {
		if(dice < 1)
			return -1;
		
		int sum = 0;
		for(int i = 0; i < dice; ++i)
			sum += rollDice(sides);
		return sum;
	}

	/**
	 * Rolls a number of dice of the same number of sides. 
	 * @return ATTACKER DOWN: The attacking player is Knocked Down.
		BOTH DOWN: Both players are Knocked Down.
		PUSHED: The defending player is pushed back one square by the blocking player. The attacking player may follow up the defender. 
		DEFENDER STUMBLES: Unless the defender uses the Dodge skill he is pushed back and then Knocked Down. If he does  use the Dodge skill 
		then he is only pushed back. The attacking player may follow up the defender. 
		DEFENDER DOWN: The defending player is pushed back and then Knocked Down in the square they are moved to. The attacking player 
		may follow up the defender. 
	 */
	public static BlockActionType rollBlockDice() 
	{
		int role = rollDice(5);
		
		// FIXME: The block dice has 6 sides, but we only have 5 actions. Which action has a bigger chance to happen?
		switch(role)
		{
		case 1:
			return BlockActionType.ATTACKER_DOWN;
		case 2:
			return BlockActionType.BOTH_DOWN;
		case 3:
			return BlockActionType.DEFENDER_DOWN;
		case 4:
			return BlockActionType.DEFENDER_STUMBLES;
		case 5:
		default:
			return BlockActionType.PUSHED;
		}
	}
	
	private static Random _generator = new Random();
}
